<template>
  <a-modal class="confirm-modal-container" :width="380" v-model="visible">
    <template slot="footer">
      <div class="footer-body middle-flex">
        <div class="no-enter middle-flex">
          <a-checkbox :checked="check" @change="check = $event.target.checked"></a-checkbox>
          <div class="text">不再提示</div>
        </div>
        <div class="btn">
          <a-button @click="visible = false">取消</a-button>
          <a-button type="primary" @click="handleOk">{{okText}}</a-button>
        </div>
      </div>
    </template>
    <div class="flex">
      <a-icon class="warning-icon" type="exclamation-circle"/>
      <div class="flex1">
        <h4 class="title">{{title}}</h4>
        <div class="info">{{content}}</div>
      </div>
    </div>
  </a-modal>
</template>

<script>
import authority from "@/utils/authority";

export default {
  data() {
    return {
      check: false,
      visible: false,
      key: "key",
      title: "",
      content: "",
      okText: "确定",
      onOk: () => {}
    };
  },
  watch: {
    "$route.name"() {
      this.visible = false;
    }
  },
  methods: {
    confirm({
      key = "",
      title = "",
      content = "",
      onOk = () => {},
      okText = "确定"
    }) {
      this.onOk = onOk;
      const user = authority.get() || {};
      const { confirmKeys = {} } = user;
      if (!key || confirmKeys[key]) return this.handleOk();
      this.visible = true;
      this.okText = okText;
      this.title = title;
      this.content = content;
      this.key = key;
    },
    async handleOk() {
      await this.onOk();
      const user = authority.get() || {};
      const { confirmKeys = {} } = user;
      confirmKeys[this.key] = !!this.check;
      user.confirmKeys = confirmKeys;
      authority.set(user);
      this.visible = false;
    }
  }
};
</script>

<style lang="less" scoped>
.footer-body {
  justify-content: space-between;
  .text {
    margin-left: 5px;
  }
}
</style>

<style lang="less">
.confirm-modal-container {
  .ant-modal-header {
    border-bottom: none;
  }
  .ant-modal-footer {
    border-top: none;
  }
  .warning-icon {
    color: #faad14;
    font-size: 20px;
    margin-right: 15px;
  }
  .title {
    font-size: 16px;
    line-height: 1.5;
  }
  .info {
    font-size: 14px;
    color: #999;
    line-height: 1.5;
    padding-top: 10px;
  }
}
</style>
